package com.example.admin.controller;

import com.example.admin.common.result.R;
import com.example.admin.common.util.UserContext;
import com.example.admin.entity.SysMenu;
import com.example.admin.model.dto.LoginDTO;
import com.example.admin.model.dto.RegisterDTO;
import com.example.admin.model.vo.LoginVO;
import com.example.admin.service.AuthService;
import com.example.admin.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "认证管理接口")
@RestController
@RequestMapping("/api/auth")
@CrossOrigin
public class AuthController {

    @Autowired
    private AuthService authService;

    @Autowired
    private MenuService menuService;

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public R<LoginVO> login(@RequestBody @ApiParam("登录信息") LoginDTO loginDTO) {
        return R.ok(authService.login(loginDTO));
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public R<Void> register(@RequestBody @ApiParam("注册信息") RegisterDTO registerDTO) {
        authService.register(registerDTO);
        return R.ok();
    }

    @GetMapping("/menus")
    public R<List<SysMenu>> getMenus() {
        Long userId = UserContext.getUser().getId();
        return R.ok(menuService.getUserMenus(userId));
    }
} 